
/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/package pl.vdl.azbest.log;

import java.awt.TextArea;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import pl.vdl.azbest.annotations.*;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

import pl.vdl.azbest.annotations.TODO;
import pl.vdl.azbest.mremote.Conf;

@TODO(dateAssigned = "24-03-2008", level = Level.OPTIONAL, target = "<br>"
		+ "Swich System.err and out to diffrent output like<br> "
		+ "SWT TextArea,File, Support for logging via Logger<br> ", done = "<br>"
		+ "System.out to TextArea, and File via Save to ...<br>"
		+ "No system err support - inpractical"
		+ "<br>Logger support via Handler<br>", responsible = Team.AZBEST)
/** Manipulating System.err and System.out */
public class LoggingTheGatheringUtil {
	{
		LoggingTheGathering.addPath(getClass().getName());
	}

	public static void changeLoggerToTextArea(final Text textArea,
			final Logger logger) {

		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				final char r = (char) b;
				Display dd = textArea.getShell().getDisplay();
				dd.syncExec(new Runnable(){
					public void run() {
						textArea.append("" + r);	
					}
					
				});
				
			}
		}

		class MyHandler extends Handler {
			private OutputStream ot;

			public void addOutputStream(TextAreaOutputStream ot) {
				this.ot = ot;
			}

			public void close() {
				try {
					ot.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			public void publish(LogRecord record) {
				String msg = "";
				msg += "CLASS: " + record.getSourceClassName()
						+ Conf.getInstance().LineS;
				msg += "MESSAGE: " + record.getMessage()
						+ Conf.getInstance().LineS;

			//	try {
			//		for (byte b : msg.getBytes()) {
					//	ot.write(b);
						
						
				//	}
			//	} catch (IOException e) {
			//		e.printStackTrace();
			//	}
			}

			public void flush() {
				try {
					ot.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		}

		MyHandler handler = new MyHandler();
		handler.addOutputStream(new TextAreaOutputStream());
		logger.addHandler(handler);

		// TextAreaOutputStream taos = new TextAreaOutputStream();
		// PrintStream ps = new PrintStream(taos);
		// System.setErr(ps);

	}

	public static void changeLoggerToTextAreaNoClassPathHandler(
			final Text textArea, final Logger logger) {

		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				char r = (char) b;
				textArea.append("" + r);
			}
		}

		class MyHandler extends Handler {
			private OutputStream ot;

			public void addOutputStream(TextAreaOutputStream ot) {
				this.ot = ot;
			}

			public void close() {
				try {
					ot.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			public void publish(LogRecord record) {
				String msg = "";
				// msg+="CLASS: "+record.getSourceClassName();
				msg += "MESSAGE: " + record.getMessage()
						+ Conf.getInstance().LineS;

				try {
					for (byte b : msg.getBytes()) {
						ot.write(b);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			public void flush() {
				try {
					ot.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		}

		MyHandler handler = new MyHandler();
		handler.addOutputStream(new TextAreaOutputStream());
		logger.addHandler(handler);

		// TextAreaOutputStream taos = new TextAreaOutputStream();
		// PrintStream ps = new PrintStream(taos);
		// System.setErr(ps);

	}

	public static void changeLoggerToDefault(Logger logger) {
		Handler[] handlers = logger.getHandlers();
		for (Handler h : handlers) {
			System.out.println("handler removed");
			logger.removeHandler(h);
		}

	}

	/**
	 * Pipeing System.out to Text swt object, like text area
	 * 
	 * @deprecated
	 */
	private static void systemErrToTextArea(final Text textArea) {
		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				char r = (char) b;
				textArea.append("" + r);
			}
		}
		TextAreaOutputStream taos = new TextAreaOutputStream();
		PrintStream ps = new PrintStream(taos);
		System.setErr(ps);
	}

	/**
	 * Pipeing System.out to Text swt object, like text area
	 * 
	 * @deprecated
	 * 
	 */
	private static void systemErrToTextAreaSwing(final TextArea textArea) {
		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				char r = (char) b;
				textArea.append("" + r);
			}
		}
		TextAreaOutputStream taos = new TextAreaOutputStream();
		PrintStream ps = new PrintStream(taos);
		System.setErr(ps);
	}

	/** Pipeing System.out to Text swt object, like text area */
	public static void systemOutToTextArea(final Text textArea) {
		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				char r = (char) b;
				textArea.append("" + r);
			}
		}
		TextAreaOutputStream taos = new TextAreaOutputStream();
		PrintStream ps = new PrintStream(taos);
		System.setOut(ps);
	}

	/** Pipeing System.out to Text swt object, like text area */
	public static void systemOutToTextAreaSwing(final TextArea textArea) {
		class TextAreaOutputStream extends OutputStream {
			public void write(int b) throws IOException {
				char r = (char) b;
				textArea.append("" + r);
			}
		}
		TextAreaOutputStream taos = new TextAreaOutputStream();
		PrintStream ps = new PrintStream(taos);
		System.setOut(ps);
	}

	/** Pipeing System.out to file */
	public static void systemOutToFile(File file) {
		try {
			PrintStream ps = new PrintStream(file);
			System.setOut(ps);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/** Pipeing System.out to file */
	public static void systemErrToFile(File file) {
		try {
			PrintStream ps = new PrintStream(file);
			System.setErr(ps);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

	}

	/** MOving out to err */
	public static void systemOutInItsRightPlace() {
		System.setOut(System.err);

	}

	public static void main(String[] args) {
		System.out.println("--");

	}

	public static String saveTXTToFile(String txt, String path) {
		File f = new File(path);
		if (f.isDirectory())
			return "This is directory";
		try {
			PrintWriter pw = new PrintWriter(f);
			pw.write(txt);
			pw.flush();
			pw.close();
		} catch (FileNotFoundException e) {
			// TODO: handle exception
		}

		return "";
	}
}
